<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">
        <el-col :span="12">
<!--          <el-form-item label="上级" prop="parentIds">-->
<!--            <el-cascader-->
<!--              v-model="temp.parentIds"-->
<!--              :options="list"-->
<!--              :props="treeProps"-->
<!--              :show-all-levels="false"-->
<!--              label="name"-->
<!--              change-on-select-->
<!--              clearable-->
<!--              style="width: 100%"-->
<!--            />-->
          <!-- </el-form-item> -->
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据源名称" prop="sourceName">
            <el-input v-model="temp.sourceName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据源类型" prop="dbType">
            <el-select v-model="temp.dbType" class="filter-item" placeholder="请选择数据源类型" >
              <el-option
                v-for="item in dictList('datasource_type')"
                :key="'type' + item.label"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status" >
            <el-radio
              v-for="item in dictList('sf')"
              :key="'type' + item.label"
              v-model="temp.status"
              :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="主机" prop="dbIp">
            <el-input v-model="temp.dbIp" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="端口号" prop="dbHost">
            <el-input v-model="temp.dbHost" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据库" prop="dbName">
            <el-input v-model="temp.dbName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户名" prop="dbUser">
            <el-input v-model="temp.dbUser" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" prop="dbPwd">
            <el-input v-model="temp.dbPwd" :type="passwordType"  />
            <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus==='create'?createData():updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createsource, updatesource, getsource } from '@/api/metadata/source'

export default {
  name: 'sourceForm',
  data() {
    return {
      rules: {
        sourceName: [{ required: true, message: '数据源名称为必填项', trigger: 'blur' }],
                          dbType: [{ required: true, message: '数据源类型为必填项', trigger: 'blur' }],
                          status: [{ required: true, message: '状态为必填项', trigger: 'blur' }],
                          dbIp: [{ required: true, message: '主机为必填项', trigger: 'blur' }],
                          dbHost: [{ required: true, message: '端口号为必填项', trigger: 'blur' }],
                          dbName: [{ required: true, message: '数据库为必填项', trigger: 'blur' }],
                          dbUser: [{ required: true, message: '用户名为必填项', trigger: 'blur' }],
                          dbPwd: [{ required: true, message: '密码为必填项', trigger: 'blur' }]
                        },
      temp: {},
      list: [],
      treeProps: {
        value: 'id',
        label: 'name',
        expandTrigger: 'hover'
      },
      textMap: {
        update: '编辑',
        create: '新建'
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      passwordType: 'password'
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
 
    },
    setList(list) {
      this.list = list
    },
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
              sourceName: undefined,
              createBy: undefined,
              createTime: undefined,
              createDept: undefined,
              updateBy: undefined,
              updateTime: undefined,
              remark: undefined,
              dbType: undefined,
              status: undefined,
              isSync: undefined,
              dbSchema: undefined,
              dbIp: undefined,
              dbHost: undefined,
              dbName: undefined,
              dbUser: undefined,
              dbPwd: undefined
            }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // 预处理提交的数据
          const parentIds = this.temp.parentIds
          if (parentIds !== undefined && parentIds !== '') {
            if (parentIds instanceof Array && parentIds.length > 0) {
              const parentId = parentIds[parentIds.length - 1]
              this.temp.parentId = parentId
            }
          }

          if (this.temp.parentIds !== undefined && this.temp.parentIds !== '') {
            this.temp.parentIds.length = 0
          }
          this.loading = true
          createsource(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.dialogFormVisible = false
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getsource(id).then(response => {
        if (response.data.code === 0) {
          this.temp = response.data.data
          if (this.temp.children !== undefined) {
            this.temp.children.length = 0
          }
          if (this.temp.parentIds !== undefined) {
            let parentIds = this.temp.parentIds.trim()
            if (parentIds.length > 0) {
              parentIds = parentIds.substr(0, parentIds.length - 1)
            }
            this.temp.parentIds = parentIds.split('\/')
          }
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const parentIds = this.temp.parentIds
          if (parentIds !== undefined && parentIds !== '') {
            if (parentIds instanceof Array && parentIds.length > 0) {
              const parentId = parentIds[parentIds.length - 1]
              this.temp.parentId = parentId
            }
          }
          if (this.temp.parentIds !== undefined && this.temp.parentIds !== '') {
            this.temp.parentIds = undefined
          }
          if (this.temp.parent !== undefined) {
            this.temp.parent = undefined
          }
          this.loading = true
          updatesource(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .show-pwd {
    position: absolute;
    right: 10px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }
</style>
